package com.weilive.api.gate.json.handler;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.api.ServerErrors;
import com.weilive.api.gate.json.protocol.CSA_OpenNotice;
import com.weilive.api.gate.json.protocol.CSQ_OpenNotice;
import com.weilive.api.service.UserSystem;
import com.weilive.core.protocol.json.JsonRequest;
import com.weilive.core.protocol.json.JsonRequestHandler;
import com.weilive.core.protocol.json.JsonResponse;

@Service
public class CSQ_OpenNotice_JsonHandler extends JsonRequestHandler {

	@Autowired
	private UserSystem userSystem;

	public Class<?> getRequestType() {
		return CSQ_OpenNotice.class;
	}

	public Class<?> getResponseType() {
		return CSA_OpenNotice.class;
	}

	public JsonResponse handleRequest(JsonRequest request) throws Exception {

		CSQ_OpenNotice params = (CSQ_OpenNotice) request.getData();
		CSA_OpenNotice response = new CSA_OpenNotice();
		
		if (params.getUserId() == null || StringUtils.isBlank(params.getCmdType())) {
			return createErrorResponse(ServerErrors.PARAM_INVALID);
		}
		int type = Integer.parseInt(params.getCmdType());

		if (type != 1 && type != 2) {
			return createErrorResponse(ServerErrors.PARAM_INVALID);
		}

	

		int status = userSystem.updateOpenStatus(params.getUserId(), type);

		response.setOpenStatus(status);

		return JsonResponse.create(response);
	}

}
